
from time import sleep

from pyx_package.script import BaseBehavior
from pyx_package.ps_paramete import PsParameter
PYX_MODULE_NAME = "timer output"

class ScriptBehavior(BaseBehavior):

    def __init__(self) -> None:
        super().__init__()
        
        self.SetExeMode("process")

        self.InitParams()

    def InitParams(self):

        self.p_interval = self.CreateParam(
            "interval", "间隔时间 /s", PsParameter.Input, float)
        
        self.p_interval.value = 1

        self.p_content = self.CreateParam(
            "content", "下发内容", PsParameter.Input, any)
        
        #下发内容
        self.p_issus_value = self.CreateParam(
            "issus_value", "输出下发内容", PsParameter.Output, any)
        
        #当前执行次数
        self.p_run_times = self.CreateParam(
            "run times", "当先执行次数", PsParameter.Output, int, 1)
        
        self.is_pause = False
    
        #创建一个信号
        self.sig_issus = self.CreateSignal("issus", "下发一次内容", issus_content = (any, "下发的对象"))

    def Start(self):
    
        while True:
            
            sleep(self.p_interval.value)

            #如果暂停就跳过
            if self.is_pause:
                continue

            #发送信号
            self.sig_issus.emit(issus_content = self.p_content.value)

            # #下发数据，等待脚本被调用
            self.Ready()

            self.p_run_times.value += 1
			
    def DoStep(self):

        #将当前的content设置给输出脚本，等待提取
        self.p_issus_value.value = self.p_content.value
        
    def Pause(self):
        '''
            暂停时处理
        '''
        self.is_pause = True
        
    def GoOn(self):
        self.is_pause = False